package com.example.ccj.b.c.ui;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.ccj.R;
import com.example.ccj.u.ImageUtils;
import com.example.ccj.u.StringUtils;
import java.util.ArrayList;

public class HeadUIAdapter extends RecyclerView.Adapter<HeadUIAdapter.HeadUiHolder> {

    ArrayList<HeadUI.HeadUIAdapterUtil> headUIAdapterUtils;
    /**
     * 构造方法
     * @param headUIAdapterUtils 内容
     */
    public HeadUIAdapter(ArrayList<HeadUI.HeadUIAdapterUtil> headUIAdapterUtils){
        this.headUIAdapterUtils = headUIAdapterUtils;
    }

    @NonNull
    @Override
    public HeadUiHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_head_ui, parent, false);
        return new HeadUiHolder(inflate);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(@NonNull HeadUiHolder holder, int position) {
        HeadUI.HeadUIAdapterUtil headUIAdpaterUtil = headUIAdapterUtils.get(position);

        //图片
        if (headUIAdpaterUtil.isNetwork) {
            ImageUtils.loadImage(holder.mImage.getContext(),headUIAdpaterUtil.urlPath,holder.mImage);
        } else {
            if (headUIAdpaterUtil.image != 0) {
                holder.mImage.setImageResource(headUIAdpaterUtil.image);
            }
        }

        //文本
        if (!StringUtils.isEmpty(headUIAdpaterUtil.text)) {
            holder.mText.setText(headUIAdpaterUtil.text);
        }

        //点击事件
        if (headUIAdpaterUtil.itemClickListener != null){
            holder.mImage.setOnClickListener(headUIAdpaterUtil.itemClickListener);
        }

        if (headUIAdpaterUtil.jiaobiao > 0) {
            holder.jiaobiao.setVisibility(View.VISIBLE);
            if (headUIAdpaterUtil.jiaobiao > 999) {
                holder.jiaobiao.setText("999");
            } else {
                holder.jiaobiao.setText(String.valueOf(headUIAdpaterUtil.jiaobiao));
            }
        } else {
            holder.jiaobiao.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemViewType(int position) {
        return super.getItemViewType(position);
    }

    @Override
    public int getItemCount() {
        return headUIAdapterUtils.size();
    }

    public static class HeadUiHolder extends RecyclerView.ViewHolder{
        ImageView mImage;
        TextView mText,jiaobiao;
        public HeadUiHolder(@NonNull View itemView) {
            super(itemView);
            mImage = itemView.findViewById(R.id.adapter_hrad_ui_iamge);
            mText = itemView.findViewById(R.id.adapter_hrad_ui_text);
            jiaobiao = itemView.findViewById(R.id.adapter_hrad_ui_jiaobiao);
        }
    }

}
